import java.util.Hashtable;
import java.util.StringTokenizer;

class P10194 {

	public static void main(String[] args) {
		int cases = Integer.parseInt(readLn());
		
		for (int k = 0; k < cases; k++) {
			process();
			if (k + 1 != cases) System.out.println();
		}
	}

	private static void process() {
		System.out.println(readLn());
		int numTeams = Integer.parseInt(readLn());
		Team[] teams = new Team[numTeams];
		Hashtable teamsTable = new Hashtable();
		
		
		for (int k = 0; k < numTeams; k++) {
			teams[k] = new Team();
			teams[k].realName = readLn();
			teams[k].name = teams[k].realName.toLowerCase();
			teamsTable.put(teams[k].name, teams[k]);
		}
		
		int games = Integer.parseInt(readLn());
		
		for (int k = 0; k < games; k++) {
			StringTokenizer st = new StringTokenizer(readLn(), "@");
			StringTokenizer st1 = new StringTokenizer(st.nextToken(), "#");
			String t1Name = st1.nextToken().toLowerCase();
			int t1Goals = Integer.parseInt(st1.nextToken());
			
			StringTokenizer st2 = new StringTokenizer(st.nextToken(), "#");
			int t2Goals = Integer.parseInt(st2.nextToken());
			String t2Name = st2.nextToken().toLowerCase();

			
			Team team1 = (Team) teamsTable.get(t1Name);
			team1.gamesPlayed++;
			team1.goalsScored += t1Goals;
			team1.goalsAgainst += t2Goals;
			
			Team team2 = (Team) teamsTable.get(t2Name);
			team2.gamesPlayed++;
			team2.goalsScored += t2Goals;
			team2.goalsAgainst += t1Goals;

			if (t1Goals == t2Goals) {
				team1.ties++;
				team2.ties++;
			} else if (t1Goals > t2Goals) {
				team1.wins++;
				team2.loses++;
			} else {
				team2.wins++;
				team1.loses++;
			}
		}
		
		sort(teams);
		
		print(teams);
		
		
	}

	
//	[a]) Team_name [b]p, [c]g ([d]-[e]-[f]), [g]gd ([h]-[i])
//
//	Where:
//
//	    * [a] = team rank
//	    * [b] = total points earned
//	    * [c] = games played
//	    * [d] = wins
//	    * [e] = ties
//	    * [f] = losses
//	    * [g] = goal difference
//	    * [h] = goals scored
//	    * [i] = goals against 
	
	private static void print(Team[] teams) {
		int length = teams.length;
		for (int k = 0; k < length; k++) {
			String answer = k+1 + ") " + teams[k].realName + " " + getPoints(teams[k]) + "p, " + teams[k].gamesPlayed + 
				"g (" + teams[k].wins + "-" + teams[k].ties + "-" + teams[k].loses + "), " + (teams[k].goalsScored - teams[k].goalsAgainst) + 
				"gd (" + teams[k].goalsScored + "-" + teams[k].goalsAgainst + ")";   
			System.out.println(answer);
		}
		
	}

	private static int getPoints(Team team) {
		return (team.wins * 3) + team.ties;
	}

	static void QuickSort(Team[] teams, int lo0, int hi0) {
		int lo = lo0;
		int hi = hi0;
		Team mid;

		if (hi0 > lo0) {

			mid = teams[(lo0 + hi0) / 2];

			while (lo <= hi) {
				while ((lo < hi0) && (compare(teams[lo],mid) > 0))
					++lo;

				while ((hi > lo0) && (compare(teams[hi], mid) < 0))
					--hi;

				if (lo <= hi) {
					swap(teams, lo, hi);

					++lo;
					--hi;
				}
			}

			if (lo0 < hi)
				QuickSort(teams, lo0, hi);

			if (lo < hi0)
				QuickSort(teams, lo, hi0);

		}
	}

//	Teams are ranked according to these rules (in this order):
//
//		   1. Most points earned.
//		   2. Most wins.
//		   3. Most goal difference (i.e. goals scored - goals against)
//		   4. Most goals scored.
//		   5. Less games played.
//		   6. Lexicographic order. 
	private static int compare(Team a, Team b) {
		if (getPoints(a) > getPoints(b)) {
			return +1;
		} else if (getPoints(a) < getPoints(b)) {
			return -1;
		} else if (a.wins > b.wins) {
			return +1;
		} else if (a.wins < b.wins) {
			return -1;
		} else if ((a.goalsScored - a.goalsAgainst) > (b.goalsScored - b.goalsAgainst)) {
			return +1;
		} else if ((a.goalsScored - a.goalsAgainst) < (b.goalsScored - b.goalsAgainst)) {
			return -1;
		} else if (a.goalsScored > b.goalsScored) {
			return +1;
		} else if (a.goalsScored < b.goalsScored) {
			return -1;
		} else if (a.gamesPlayed < b.gamesPlayed) {
			return +1;
		} else if (a.gamesPlayed > b.gamesPlayed) {
			return -1;
		} else return b.name.compareTo(a.name);
		
	}

	static void swap(Team[] a, int i, int j) {
		Team T;
		T = a[i];
		a[i] = a[j];
		a[j] = T;
	}




	private static void sort(Team[] teams) {
		QuickSort(teams, 0, teams.length - 1);
	}
	
	

	static String readLn() {
		String newLine = System.getProperty("line.separator");
		StringBuffer buffer = new StringBuffer();
		int car = -1;
		try {
			car = System.in.read();
			while ((car > 0) && (car != newLine.charAt(0))) {
				buffer.append((char) car);
				car = System.in.read();
			}
			if (car == newLine.charAt(0))
				System.in.skip(newLine.length() - 1);
		} catch (java.io.IOException e) {
			return (null);
		}
		if ((car < 0) && (buffer.length() == 0))
			return (null);
		return (buffer.toString()).trim();
	}

}

class Team {
	String name;
	String realName;
	int wins;
	int loses;
	int ties;
	int goalsScored;
	int goalsAgainst;
	int gamesPlayed;
}